package com.ambow.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.entity.News;
import com.ambow.service.NewsService;
import com.ambow.service.impl.NewsServiceImpl;
import com.ambow.utils.PageResults;

/**
 * Servlet implementation class NewsServlet
 */
@WebServlet("/NewsServlet")
public class NewsServlet extends HttpServlet {
	NewsService newsService = new NewsServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String status = request.getParameter("status");
		if (status.equals("query")) {
			query(request, response);// 查询全部新闻
		}
	}

	// 查询全部新闻
	private void query(HttpServletRequest request, HttpServletResponse response) {
		try {
			String hql = "from News where status = 1 order by publishTime desc";
			String countHql = "select count(*) from News where status=1 order by publishTime desc";
			Integer pageNo = Integer.valueOf(request.getParameter("currentPage"));
			Integer pageSize = 2;
			PageResults<News> pageResults = newsService.getPageByFetchedHql(hql, countHql, pageNo, pageSize);
			if (pageResults != null) {
				request.setAttribute("pageResults", pageResults);
				request.getRequestDispatcher("pages/about.jsp").forward(request, response);
			}
		} catch (Exception e) {
		}
	}

}
